package user

import (
	"fmt"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/admin"

	"github.com/gin-gonic/gin"
)

func DeleteUser(c *gin.Context) {
	requestDataStruct := struct {
		Id int `json:"id"`
	}{}

	c.ShouldBindJSON(&requestDataStruct)

	//删除用户
	var adminStruct admin.AdminStruct
	if err := model.DeleteModelData(
		model.Db,
		adminStruct.TableName(),
		map[string]interface{}{
			"id": requestDataStruct.Id,
		},
		&adminStruct,
	); err != nil {
		controller.Fail(c, fmt.Sprintf("删除用户信息失败%s", err.Error()))
		return
	}

	//删除token
	var adminTokenStruct admin.AdminTokenStruct
	if err := model.DeleteModelData(
		model.Db,
		adminTokenStruct.TableName(),
		map[string]interface{}{
			"admin_id": requestDataStruct.Id,
		},
		&adminTokenStruct,
	); err != nil {
		controller.Fail(c, fmt.Sprintf("删除用户信息失败1%s", err.Error()))
		return
	}

	controller.Success(c, "")
}
